
/**
 * Created by wxnacy on 2015/8/12.
 */
$(function() {
    //setInterval("getVideoUploadProgress()", 1000);
    //$('body').everyTime('1s',getVideoUploadProgress());
    //submitUploadVideoForm()
    //初始化上传视频组件
    initUploadVideo()
});
/**
 * 修改页面中初始化视频图片
 * @param id
 * @param videoId
 */
function initEditVideoPic(id,videoId){
    if(videoId == ''){
        return;
    }
    //if(videoId == ''|| videoId.indexOf(",")){
    //    return;
    //}
    var $video = $('#'+id);
    var $viewImg = $video.find('img[data-theme-name="view-image"]')
    $viewImg.attr('src','/images/upload_wait.jpg')
    var url = "/vrs/api/getVideo.jsn?videoId="+videoId;
    $.ajax({
        type: "get",
        dataType: "json",
        url: url,
        error: function () {
            $viewImg.attr('src','/images/upload_error.jpg')
        },
        success: function (data) {
            var code = data.code;
            if(code == 0){
                data = data.data;
                var status = data.status
                if(status == 10){
                    $('.isdisplay').show();
                    $viewImg.attr('src',data.img)
                }else if(status == 20){
                    $('.isdisplay').hide();
                    $viewImg.attr('src','/images/upload_error.jpg')
                }else if(status == 30){
                    $('.isdisplay').hide();
                    $viewImg.attr('src','/images/upload_wait.jpg')
                }

            }else{
                $viewImg.attr('src','/images/upload_wait.jpg')
            }


        }
    })
}
//上传传视频组件
function initUploadVideo(){
    //开始先隐藏视频类组件
    initUploadVideoRadio()
    //初始化上传视频css
    initUploadVideoCss()
    //初始化提交视频功能
    initUploadVideoForm()
}
//开始先隐藏视频类组件
function initUploadVideoRadio(){

    var $uploadVideoDiv = $('div[data-theme-name="upload-video-div"]')
    var $liveVideoDiv = $('div[data-theme-name="live-video-div"]')
   // $uploadVideoDiv.hide()
    //$liveVideoDiv.hide()
    //选择视频分类的按钮
    $('div[data-theme-name="video-type-div"]').find('label').each(function(){
        $(this).bind('click',function() {
            var value = $(this).find('input').val()
            //console.log(value)
            if (value == 4 || value == 5) {
                $uploadVideoDiv.hide()
                $liveVideoDiv.fadeIn()
            } else {
                $liveVideoDiv.hide()
                $uploadVideoDiv.fadeIn()
            }
        })
    })
}
//初始化提交视频功能
function initUploadVideoForm(){
    var $label = $('label[data-theme-name="upload-video-label"]')
    var labelId = $label.attr('id')
    var $span = $label.find('span')
    var $inputFile = $label.find('input[type=file]');//提交file组件
    $inputFile.change(function(){
        var $file = $(this)
        var $copyFile = $(this).clone(true)
        //等待图片等组件
        var $viewImg = $file.siblings('img[data-theme-name="view-image"]')
        var $waitImg = $file.siblings('img[data-theme-name="wait-image"]')
        $waitImg.fadeIn()
        $span.fadeIn()
        var value = $(this).val()
        var index = value.lastIndexOf("\\")+1
        value = value.substring(index,value.lastIndexOf("."))
        var url = $(this).parent().attr('data-url')
        url = url + "?videoName="+value
        //console.log(value,url)
        $.ajax({
            type: "get",
            dataType: "json",
            url: url,
            error: function () {
                var msg = '上传出错,请稍后再试';
                var type = 'error';
                var $layout = 'topRight';
                notyRestart(msg,type,$layout);
            },
            success: function (data) {
                console.log('getUploadUrl',data)
                var code = data.code;
                if(code == 0){
                    //获取乐视返回相关数据
                    data = data.data
                    var upload_url = data.upload_url;
                    var progress_url = data.progress_url;
                    var video_id = data.video_id;
                    //添加更新进度地址
                    $label.attr('progress-url',progress_url)

                    //初始化上传视频的form
                    var html = '<form method="post" enctype="multipart/form-data"></form>';
                    var $form = $(html);
                    $form.attr('action',upload_url)
                    $form.append($file);
                    $label.append($copyFile)
                    //console.log('form',$form.attr('action'),$form.find('input').val())
                    //执行定时任务
                    var proInt = setInterval("getUploadProgress('"+labelId+"')", 2000);
                    //ajax提交
                    $form.ajaxSubmit(function(data){
                        $waitImg.fadeOut()
                        var code = data.code
                        console.log('result',data);
                        if(code == 0){
                            var msg = '上传成功,正在处理中';
                            var type = 'success';
                            var $layout = 'topRight';
                            notyRestart(msg,type,$layout);

                            //记录源id
                            $('input[name="video.source"]').val(video_id)
                            //getVideo(labelId,video_id)
                            //$endImg.fadeIn()
                            $viewImg.attr('src','/images/upload_wait.jpg');
                            $label.find('span').text('0%')
                        }else{
                            var msg = data.message;
                            var type = 'error';
                            var $layout = 'topRight';
                            notyRestart(msg,type,$layout);
                        }
                        clearInterval(proInt);
                        $span.fadeOut()
                        $form.attr('action','/vrs/video/getUploadVideoUrl.jsn');
                    })
                }else{
                    var msg = data.message;
                    var type = 'error';
                    var $layout = 'topRight';
                    notyRestart(msg,type,$layout);
                }
            }
        })
    })
}

//获取视频详情
function getVideo(id,videoId){
    var $label = $('#'+id)
    var url = "/vrs/api/getVideo.jsn?videoId="+videoId;
    $.ajax({
        type: "get",
        dataType: "json",
        url: url,
        error: function () {
            var msg = '获取视频详情失败';
            var type = 'error';
            var $layout = 'topRight';
            notyRestart(msg, type, $layout);
        },
        success: function (data) {
            console.log('video',data)
            var code = data.code
            if(code == 0){
                var imgUrl = data.img;
                $label.find('img[data-theme-name="view-image"]').attr('src',imgUrl)
            }else{

            }
        }
    })
}
//获取上端进度
function getUploadProgress(id){
    var $label = $('#'+id)
    //console.log(id)
    var url = $label.attr('progress-url')
    url = '/vrs/api/getUpdateVideoProgress.jsn?url='+url;
    //console.log(url)
    if(url.length>0){
        $.ajax({
            type: "get",
            dataType: "json",
            url: url,
            error: function () {
                console.log('error');
            },
            success: function (data) {
                //console.log(data);
                var status = data.status;
                if(status == 200){
                    var result = data.result;
                    var totalSize = result.totalSize;
                    var uploadSize = result.uploadSize;
                    var progress  = parseInt((uploadSize/totalSize)*100);
                    if(progress==99){
                        progress = 100;
                    }
                    console.log(progress);
                    var $span = $label.find('span')
                    $span.text(progress+'%')
                }

            }
        });
    }else{
        console.log('wait');
    }
}
//初始化上传视频css
function initUploadVideoCss(){
    var $label = $('label[data-theme-name="upload-video-label"]')
    var $inputFile = $label.find('input[type=file]');//提交file组件
    var $img = $label.find('img[data-theme-name="view-image"]');//呈现图片
    var $waitImg = $label.find('img[data-theme-name="wait-image"]');//呈现图片
    var $span = $label.find('span')

    var w = $img.width();
    var h = $img.height();
    $waitImg.css({"z-index":"inherit","position":"relative","margin-left":"-140px","display":"none"});
    $inputFile.css({"width":w,"height":h,"margin-top":"-"+parseInt(h)+"px","opacity":"0"})
    //var $endImg = $label.find('img[data-theme-name="end-image"]');
    //$endImg.css({"width":w,"height":h,"z-index":"inherit","position":"relative","margin-left":"-"+parseInt(w)+"px","display":"none"});
    $span.hide()
}









function submitUploadVideoForm(){
    $("input[name='video_file']").change(function(){
        var name = $(this).val();
        console.log(name);
        var $uploadVideoForm = $(this).parent();
        var url = $uploadVideoForm.attr('action')+"?videoName="+name;
        console.log(url);
        var $file = $(this).clone(true)
        $.ajax({
            type: "get",
            dataType: "json",
            url: url,
            error: function () {
                alert('error')
            },
            success: function (data) {
                console.log(data)
                var code = data.code;
                //var status =

                if(code == 0){
                    data = data.data;
                    var upload_url = data.upload_url;
                    var progress_url = data.progress_url;
                    var video_id = data.video_id;
                    $uploadVideoForm.attr('action',upload_url);
                    //将form设置唯一标示id
                    $uploadVideoForm.attr('id','form_'+video_id);
                    $uploadVideoForm.attr('progress_url',progress_url);
                    //获取到url开始定时任务更新进度条
                    var html = '<div class="progress progress-striped active mb25" id="'+video_id+'">'+
                        '<div class="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" data-percent="0"></div>'+
                        '</div>';
                    var $Progress = $(html);
                    var $uploadInput = $uploadVideoForm.find('input[name="video_file"]');
                    $uploadInput.after($Progress);
                    //执行定时任务
                    var proInt = setInterval("getVideoUploadProgress("+video_id+")", 2000);
                    $uploadVideoForm.ajaxSubmit(function(data){
                        console.log(data);
                        clearInterval(proInt);
                        $Progress.remove();
                        $uploadVideoForm.attr('action','/vrs/video/getUploadVideoUrl.jsn');
                    })
                }else{
                    swal('操作失败', data.message, 'error');
                }
            }
        });
    });
}

/**
 * 获取进度条
 * @param video_id
 * @param progress_url
 */
function getVideoUploadProgress(video_id){

    var $form  = $('#form_'+video_id);
    var url = $form.attr('progress_url');;//$uploadProgress.attr('data-url');
    url = '/vrs/video/getUpdateVideoProgress.jsn?url='+url;
    if(url.length>0){
        $.ajax({
            type: "get",
            dataType: "json",
            url: url,
            error: function () {
                console.log('error');
            },
            success: function (data) {
                //console.log(data);
                var status = data.status;
                if(status == 200){
                    var result = data.result;
                    var totalSize = result.totalSize;
                    var uploadSize = result.uploadSize;
                    var progress  = parseInt((uploadSize/totalSize)*100);
                    if(progress==99){
                        progress = 100;
                    }
                    console.log(progress);
                    $('#'+video_id).attr('aria-valuenow',10);
                    $('#'+video_id).attr('data-percent',progress);
                }

            }
        });
    }else{
        console.log('wait');
    }
}


